package GUI.view.Calendar;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

//import sun.util.calendar.ZoneInfo;

public class MyCalen extends JPanel {

	private static final long serialVersionUID = 1L;
	private static JPanel jContentPane = null;
	private JButton jButton = null;
	private DatePicker dp;
	private JTextField dateText = null;
	private JDialog dlg;
	private Point origin = new Point();

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Choose date");
			jButton.setBounds(new Rectangle(74, 68, 34, 10));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					onButtonClick(e);
				}
			});
		}
		return jButton;
	}

	private static String dateToString(final Date dt) {
		if (null != dt) {
			String DATE_FORMAT_SHORT = "yyyy-MM-dd";
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_SHORT);
			// TimeZone zone = new ZoneInfo("GMT", 0);
			// sdf.setTimeZone(zone);
			// return sdf.format(dt);
			return DateFormat.getDateInstance(DateFormat.LONG).format(dt);
		}
		return null;
	}

	final class Listener extends ComponentAdapter {

		public void componentHidden(final ComponentEvent evt) {
			final Date dt = ((DatePicker) evt.getSource()).getDate();
			if (null != dt)
				dateText.setText(dateToString(dt));
			dlg.dispose();
		}

	}

	private static Date stringToDate(final String s) {
		try {
			return DateFormat.getDateInstance(DateFormat.LONG).parse(s);
		} catch (ParseException e) {
			return null;
		}
	}

	private void onButtonClick(final java.awt.event.ActionEvent evt) {
		if ("".equals(dateText.getText()))
			dp = new DatePicker();
		else
			dp = new DatePicker(stringToDate(dateText.getText()));
		dp.addComponentListener(new Listener());

		final Point p = dateText.getLocationOnScreen();
		p.setLocation(p.getX(), p.getY() - 1 + dateText.getSize().getHeight());

		dlg = new JDialog(new JFrame(), true);

		dlg.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				origin.x = e.getX();
				origin.y = e.getY();
			}
		});
		dlg.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent e) {
				Point p = dlg.getLocation();
				dlg.setLocation(p.x + e.getX() - origin.x, p.y + e.getY()
						- origin.y);
			}
		});

		dlg.setLocation(p);
		dlg.setResizable(false);
		dlg.setUndecorated(true);
		dlg.getContentPane().add(dp);
		dlg.pack();
		dlg.setVisible(true);
	}

	/**
	 * This method initializes date
	 * 
	 * @return javax.swing.JTextField
	 */
	public JTextField getDate() {
		if (dateText == null) {
			dateText = new JTextField();
			dateText.setPreferredSize(getJButton().getPreferredSize());
			dateText.setBounds(new Rectangle(68, 24, 115, 22));
		}
		return dateText;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				frame.setSize(300, 200);
				
				frame.setTitle("JFrame");
				
				MyCalen thisClass = new MyCalen();
				frame.getContentPane().add(thisClass);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public MyCalen() {
		this.setLayout(new FlowLayout());
		this.add(getDate());
		this.add(getJButton());
		
	}

}
